import React from "react";
import Button from "@material-ui/core/Button";
import $ from "jquery";

import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogTitle from "@material-ui/core/DialogTitle";
import GridForm from "./GridForm"

// props: fields,title open,setOpen,handleSubmit || form
export default function FormDialog(props) {
	
	var formId=props.form&&props.form.id||"form_"+(Math.random()+"").substring(2)
	const handleClose = () => {
		console.log($("#"+formId))
		props.setOpen(false);
	};
	const handleSubmit = async () => {
		// console.log($("#"+formId))
		// props.setOpen(false);
		return await props.handleSubmit($("#"+formId))
	};
	return (
		<Dialog open={props.open} onClose={handleClose} aria-labelledby="form-dialog-title" maxWidth={props.maxWidth||"md"} fullWidth>
			<DialogTitle id="form-dialog-title" onClose={handleClose}>{props.title}</DialogTitle>
			<form id={formId} {... props.form} style={{margin:"0 1em"}}>
			<GridForm fields={props.fields}></GridForm>
			</form>
			<DialogActions>
				<Button onClick={handleClose} color="secondary">
					取消
				</Button>
				<Button onClick={handleSubmit} color="primary">
					确定
				</Button>
			</DialogActions>
		</Dialog>
	);
}
